require "rubygems"
require "amadeusdsl/connection"
require "amadeusdsl/connection"
require "amadeusdsl/lookup"

module Amadeusdsl
  module Lookup

    # Look up is a singleton object
    extend self
    
        # return all airports
        def ListAirports()
          l = Amadeusdsl::Connection.put('P')
          return l
        end
        
        # return the airline code based on the given name
        def Airline(name)
            l = Amadeusdsl::Connection.put('A')
            i = nil
            l.each do
              |x|
              if (x.include? name)
                i = x
              end
            end
            if i == nil 
                puts "Could not find "+name+" as an Airline"
                return i
            end
            return i[0,3]
        end

        # returns the airport code based on the given name
        def Airport(name)
            l =  Amadeusdsl::Connection.put('P')
            ports = l.find_all {|x| x.include? name}
            if ports.length == 0 
                puts "Could not find "+name+" as an Airport"
                return nil
            end
            if ports.length == 1
                return ports[0][0,3]
            end
            if ports.length > 1
              puts "Multiple airports found at " + name + ", please select one to continue."
              i = 1
              ports.each do
                |airport|
                puts "[#{i}] " + airport[0,20] + " "+ airport[0,3]
                i = i+1
              end
              select = gets
              while (select.to_i < 0 || select.to_i > ports.length) 
                select = gets
              end
              return ports[select.to_i-1][0,3]
            end
        end
        
        # returns the airport full name based on the code
        def AirportName(code)
          l = Amadeusdsl::Connection.put('P')
          res = nil
          l.each do 
            |x| 
            if (x[0,3] == code) 
              res = x[3,20] 
            end
          end
          
          if res == nil
            puts "Could not find the airport with code " + code +"."
            return res
          end
          return res
        end
        
        # returns the airline full name based on the code
        def AirlineName(code)
          l = Amadeusdsl::Connection.put('A')
          res = nil
          l.each do 
            |x| 
            if (x[0,3] == code) 
              res = x[3,30] 
            end
          end
          
          if res == nil
            puts "Could not find the airline with code" + code +"."
            return res
          end
          return res
        end
        
        # Returns the two airports of this flight. Formatted as "City (CODE)"
        def FlightNumber (flight)
          l = Amadeusdsl::Connection.put("F"+flight)[0]  
          res = Hash.new 
          res["from"] = AirportName(l[0,3]).strip + " ("+l[0,3]+")"
          res["to"] = AirportName(l[3,3]).strip + " ("+l[3,3]+")"
          res["code"] = l
          return res
        end
        
        # days of the week
        @days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
        
        # returns the days of the week on which this flight flies, in an array
        def DaysOfWeek(flight)
           l = Amadeusdsl::Connection.put("W"+flight)[0]
           if (l == "FN")
             puts "There is no flight with code " + flight + "."
           end
           r = []
           for i in 0..7
             if (l[i,1] == "Y")
               r << @days[i]
             end
           end
           res = Hash.new
           res = {"result" => r, "code" => l}
           
           return res
        end
       
  end
end

